import subprocess
import sys
import time
import threading


def run_server():
    """运行服务器"""
    try:
        process = subprocess.Popen([
            sys.executable, "main.py"
        ])
        return process
    except Exception as e:
        print(f"启动服务器失败: {e}")
        return None


def main():
    print("🚀 启动智能零售平台API服务...")
    server_process = run_server()

    if server_process:
        print("✅ 服务器已启动")
        print("📍 访问: http://localhost:8000")
        print("⏹️  按 Enter 键停止服务...")

        try:
            input()  # 等待用户输入
        except KeyboardInterrupt:
            pass

        print("🛑 正在停止服务器...")
        server_process.terminate()
        server_process.wait()
        print("👋 服务器已停止")


if __name__ == "__main__":
    main()